//
//  NSMutableAttributedString+NXAdd.m
//  NCube
//
//  Created by kepuna on 2017/3/21.
//  Copyright © 2017年 junjie.liu. All rights reserved.
//

#import "NSMutableAttributedString+NXAdd.h"
#import "UIImage+NXAdd.h"

@implementation NSMutableAttributedString (NXAdd)

+ (instancetype)nc_attributedStringWithNSString:(NSString *)string  image:(UIImage *)image imageRect:(CGRect)imageRect index:(NSUInteger)index{

    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:string];
    //创建一个富文本附件
    NSTextAttachment *attch = [[NSTextAttachment alloc] init];
    attch.bounds = imageRect;
    attch.image = image;
    // 创建带有图片的富文本
    NSAttributedString *attributedString = [NSAttributedString attributedStringWithAttachment:attch];
    [attr insertAttributedString:attributedString atIndex:index];
    return attr;
}


+ (void)nc_attributedStringWithNSString:(NSString *)string imageCorner:(UIImage *)imageCorner imageRect:(CGRect)imageRect index:(NSUInteger)index finishBlock:(void (^)(NSMutableAttributedString *))finishBlock {
    
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:string];
    //创建一个富文本
    NSTextAttachment *attch = [[NSTextAttachment alloc] init];
    attch.bounds = imageRect;
    [UIImage nx_drawCornerWithImage:imageCorner size:imageRect.size finishBlock:^(UIImage *image) {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            attch.image = image;
            // 创建带有图片的富文本
            NSAttributedString *attributedString = [NSAttributedString attributedStringWithAttachment:attch];
            [attr insertAttributedString:attributedString atIndex:index];
            if (finishBlock) {
                finishBlock(attr);
            }

        });
        
    }];
}

@end
